<?php

namespace App\Ma\Entities;

/**
 * 矩阵助手
 * Class MatrixUtils
 * @package App\Ma\Entities
 */
class MatrixUtils
{
    /**
     * 从牌值重获取其中各个值
     * 获取第1位数值：$value >> 12
     * 获取第2位数值：($value >> 8) & 0x0f     是否鬼牌
     * 获取第3位数值：($value >> 4) & 0x0f     牌型、万、筒...
     * 获取第4位数值：$value & 0x0f            牌值
     * @param int $value
     * @param int $index
     * @return int
     */
    public static function getValueIndex(int $value, int $index = 0)
    {
        switch ($index) {
            case 0;
                return $value >> 12;
            case 1;
                return ($value >> 8) & 0x0f;
            case 2;
                return ($value >> 4) & 0x0f;
            case 3;
                return $value & 0x0f;
        }
    }


    /**
     * 通过牌型和牌面值转换为牌
     * @param int $type
     * @param int $value
     * @return float|int
     */
    public static function generateHexValue(int $type, int $value)
    {
        $isGui = 0;
        if ($type === Matrix::TYPE_GUI) { // 鬼牌
            $isGui = 1;
        }
        return hexdec("{$isGui}{$type}{$value}");
    }

    /**
     * 检测矩阵中元素是否全部为0
     * @param array $matrix
     * @return bool
     */
    public static function checkMatrixAllElemEqualZero(array $matrix): bool
    {
        foreach ($matrix as $type => $list) {
            foreach ($list as $key => $count) {
                if ($count > 0) {
                    return false;
                }
            }
            if ($type >= Matrix::TYPE_SAN) {
                // 后续的鬼牌
                break;
            }
        }
        return true;
    }

    /**
     * 检测矩阵中元素是否全部为0
     * @param array $matrix
     * @return array
     */
    public static function canAnGangValues(array $matrix): array
    {
        $values = [];
        foreach ($matrix as $type => $list) {
            foreach ($list as $key => $count) {
                if ($count >= 4) {
                    $values[] = $key + 1;
                }
            }
            if ($type >= Matrix::TYPE_SAN) {
                // 后续的鬼牌
                break;
            }
        }
        return $values;
    }

    /**
     * 矩阵转换为可读字符串
     * @param array $matrix
     * @return string
     */
    public static function matrixConvertToInfo(array $matrix): string
    {
        $info = "{";
        foreach ($matrix as $type => $list) {
            foreach ($list as $key => $count) {
                while ($count > 0) {
                    switch ($type) {
                        case Matrix::TYPE_ZI: // 字牌
                            $info .= Matrix::ZI_PAI[$key];
                            break;
                        case Matrix::TYPE_SAN: // 三元
                            $info .= Matrix::SAN_YUAN[$key];
                            break;
                        case Matrix::TYPE_GUI: // 鬼牌
                            $info .= " 鬼牌";
                            break;
                        default: // 花牌
                            $info .= ($key + 1) . Matrix::Types[$type];
                    }
                    $count--;
                }

            }
        }
        $info .= "}";
        return $info;
    }


    /**
     * 去除顺子
     * @param array $matrix
     * @return array
     */
    public static function removeThreeLinkCards(array $matrix)
    {
        foreach ($matrix as $type => $list) {
            foreach ($matrix[$type] as $key => $count) {
                $count = $matrix[$type][$key]; //解决$matrix[$type][$value + 1]等造成的影响
                if ($key >= 7) {
                    continue;
                }
                while ($count > 0) {
                    if ($matrix[$type][$key + 1] > 0 && $matrix[$type][$key + 2] > 0) {
                        $matrix[$type][$key]--;
                        $matrix[$type][$key + 1]--;
                        $matrix[$type][$key + 2]--;

//                        array_push($this->shun, [
//                            MatrixUtils::generateHexValue($type, $key + 1),
//                            MatrixUtils::generateHexValue($type, $key + 2),
//                            MatrixUtils::generateHexValue($type, $key + 3),
//                        ]);
                    }
                    $count--;
                }

            }
            if ($type >= Matrix::TYPE_TIAO) {
                // 后续的非花牌无顺子
                break;
            }
        }
        return $matrix;
    }

    /**
     * 移除刻子
     * @param array $matrix
     * @return array
     */
    public static function removeTheSameThreeCards(array $matrix)
    {
        foreach ($matrix as $type => $list) {
            foreach ($list as $key => $count) {
                if ($count >= 3) {
                    $matrix[$type][$key] -= 3;
//                        array_push($this->handKey, MatrixUtils::generateHexValue($type, $key + 1));
                }
            }
            if ($type >= Matrix::TYPE_SAN) {
                // 后续的鬼牌
                break;
            }
        }
        return $matrix;

    }
}